package com.jzo2o.market.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponService couponService;

    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivitySatus() {
        //1.查询优惠券活动列表
        LambdaQueryWrapper<Activity> queryWrapper = Wrappers.<Activity>lambdaQuery();
        queryWrapper.in(Activity::getStatus, ActivityStatusEnum.NO_DISTRIBUTE.getStatus(), ActivityStatusEnum.DISTRIBUTING.getStatus());
        List<Activity> activityList = activityService.list(queryWrapper);
        //2.遍历列表
        if (CollUtils.isEmpty(activityList)) {
            return;
        }
        for (Activity activity : activityList) {
            // 获取当前时间
            long currentTime = System.currentTimeMillis();
//            activity.getDistributeStartTime()
//            if (  ) {
//
//            }
        }
        //3.根据当前时间进行判断

        //3.1.如果是 待生效 到达开始时间就变为 进行中

        //3.2.如果是 待生效或者进行中 到过期时间改为 已失效


    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {

    }


}
